from numpy import linspace,zeros,log,matrix,floor
from matplotlib.pyplot import plot,subplot,title,show,figure
from numpy.random import random
from numpy.linalg import norm

t0=0;
tFinal=1;
T=(10**4);t=linspace(t0,tFinal,T)
dt=t[1]-t[0]
l=6;
argumento='[0,0;0,'
argumento+=str(l)
argumento+=']'
J=matrix(argumento)

for j in range(10):
    norma=[]
    delta=random(2)
    delta=delta.reshape(2,1)
    norma.append(norm(delta))
    norma[0]=1;
    delta=delta/norm(delta)
    print(delta.reshape(1,2))   
    y=zeros(T)
    Y=zeros(T)

    fracao=0
    normalizacao=T*(10**-2)
    print(normalizacao)
    validos=floor(fracao*T)
    print(validos)
    for i in range(1,T):
        delta=delta+dt*J*delta 
        norma.append(norm(delta))
        y[i]=((i-1)/(i))*y[i-1]+2*log(norma[i]/norma[i-1])
        if i>validos:
            Y[i]=((i-1)*Y[i-1]+y[i])/i;
        
        if i%normalizacao==0:
            delta=delta/norm(delta)
            norma[i]=1;

    print(delta.reshape(1,2))
    print('\n')
    figure(j+1)
    subplot(2,2,1)    
    title('y '+str(delta.reshape(1,2)))
    plot(t,y)
    subplot(2,2,2)
    title('Y')
    plot(t,Y)
    subplot(2,2,3)
    title('erro y')
    plot(t,abs(y-l*t))
    subplot(2,2,4)
    title('erro Y')
    plot(t,abs(Y-0.5*l*t))
    show()